/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 02.01.2006
 *
 *************************************************/
package fightplan.persistence;

import java.io.File;
import java.util.List;

import javax.swing.JOptionPane;

import fightplan.business.Fighter;
import fightplan.business.Tournament;
import fightplan.core.FPExceptionHandler;

public class ExcelImport {

	public static boolean imprtFightersAndTeams(Tournament tournament, File[] files) {
		Tournament temp;
		// cheap clone (save und load)
		try {
			File tempFile = File.createTempFile("b4import", null);
			FilePersistence.save(tempFile, tournament, false);
			temp = FilePersistence.load(tempFile, false);
			temp.getFighters().clear();
		} catch (Exception x) {
			x.printStackTrace();
			FPExceptionHandler.instance().showWarning("Import aus technsichen Gründen fehlgeschlagen.\n",
					"Interner Fehler");
			return false;
		}

		try {
			for (File file : files) {
				imprtFightersAndTeams(temp, file);
			}
		} catch (Exception x) {
			x.printStackTrace();
			JOptionPane.showMessageDialog(null, "Import fehlgeschlagen: " + x.getMessage(), "Fehler",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}

		// do checks

		List<Fighter> fighters = temp.getFighters();
		String error = "";
		for (int i = 0; i < fighters.size(); i++) {
			String name1 = fighters.get(i).getName().trim();
			for (int j = i + 1; j < fighters.size(); j++) {
				String name2 = fighters.get(j).getName().trim();
				if (name1.equalsIgnoreCase(name2)) {
					error += "Name doppelt: " + name1 + "\n";
				}
			}
		}

		for (int i = 0; i < fighters.size(); i++) {
			String name1 = fighters.get(i).getName().trim();
			for (int j = 0; j < tournament.getFighters().size(); j++) {
				String name2 = tournament.getFighters().get(j).getName().trim();
				if (name1.equalsIgnoreCase(name2)) {
					error += "Name bereits vorhanden: " + name1 + "\n";
				}
			}
		}

		if (error.length() > 0) {
			FPExceptionHandler.instance().showWarning("Teilnehmer doppelt oder bereits vorhanden:\n\n" + error,
					"Import fehlgeschlagen");
			return false;
		} else {
			// do it again for the real tournament
			for (File file : files) {
				try {
					imprtFightersAndTeams(tournament, file);
				} catch (Exception e) {
					// Sollte nicht mehr vorkommen, wir haben den Import ja
					// schon mal
					// gemacht.
					e.printStackTrace();
				}
			}
			return true;
		}
	}

	private static void imprtFightersAndTeams(Tournament tournament, File file) throws Exception {
		switch (tournament.getMode()) {
		case SKRKYU:
			ExcelSkrImport2011.imprtFightersAndTeams(tournament, file);
			break;
		case SKRKYU_KINDER:
			ExcelSkrKinderImport2012.imprtFightersAndTeams(tournament, file);
			break;
		case IPPONSHOBU:
			ExcelIpponShobuImport.imprtFightersAndTeams(tournament, file);
			break;
		case FUJI2008:
			ExcelFujiImport.imprtFightersAndTeams(tournament, file);
			break;
		default:
			ExcelGenericImport.imprtFightersAndTeams(tournament, file);
			break;
		}

	}

}
